// pages/mall/goodsSearch/index.js

const $api = require('../../../../request/api').API;
import Toast from '../../../../miniprogram_npm/@vant/weapp/toast/toast';
import { clearShopCartStorage, formatGoodsList } from '../../../../utils/hooks';
import { getFormatDays } from '../../../../utils/util';
const app = getApp();
const { menuBtnTop, menuBtnHeight, menuBtnWidth, menuBtnLeft } = app.globalData;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    menuBtnTop,
    menuBtnHeight,
    menuBtnWidth,
    menuBtnLeft: menuBtnLeft - 5,
    listData: [],
    pageNo: 1,
    pageSize: 20,
    totalPage: 1,
    queryType: '',
    btnId: '',
    name: '',
    empty: false,
    loading: false,
    collect: '',
    dateList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad({ keyword }) {
    const eventId = app.globalData['btnList.eventId'];
    const btnId = app.globalData['btnList.btnId'];
    const queryType = app.globalData['btnList.queryType'];
    const beeperId = app.globalData['btnList.beeperId'];
    const modifyData = {
      name: keyword,
      btnId,
      beeperId,
      queryType
    };
    if (eventId == 18) {
      // eventId为18表示挂号对接,需要日期
      const dateList = getFormatDays(7);
      modifyData.dateList = dateList;
      modifyData.dateId = dateList[0].id;
      app.globalData['dock.date'] = dateList[0].date;
    }
    this.setData(modifyData);
    this.getList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},
  // 获取数据
  async getList() {
    let { pageNo, pageSize, listData, queryType, name, btnId, beeperId, collect } = this.data;
    const params = {
      pageNo,
      pageSize,
      queryType,
      name,
      btnId,
      beeperId,
      collect
    };
    const modifyData = { empty: false };
    if (pageNo > 1) {
      modifyData.loading = true;
    } else {
      modifyData.listData = [];
      Toast.loading({
        message: '数据获取中'
      });
    }
    this.setData(modifyData);
    try {
      let res = await $api.getGoodsListByName(params);
      let { list, totalPage } = res.data.data;
      list = list || [];
      formatGoodsList(list);
      listData = pageNo == 1 ? list : listData.concat(list);
      this.setData({
        totalPage,
        listData,
        empty: listData.length ? false : true
      });
      Toast.clear();
    } finally {
      this.setData({
        loading: false
      });
    }
  },
  // 搜索
  handleSearchGoods(e) {
    const { collect } = e.currentTarget.dataset;
    const modifyData = { pageNo: 1 };
    collect !== undefined && (modifyData.collect = collect);
    this.setData(modifyData);
    this.getList();
  },
  // 输入
  handleInput(e) {
    this.setData({
      name: e.detail
    });
  },
  // 去产品详情
  toGoodsDetail(e) {
    const { item, open } = e.currentTarget.dataset;
    console.log(item);
    const { goodsId, id, specDisplayType } = item;
    if (specDisplayType == 1) {
      // 跳转到规格页面
      wx.navigateTo({
        url: '/subcontract/home/pages/goodsSpec/index?goodsId=' + goodsId
      });
      return;
    }
    // 先清空购物车
    clearShopCartStorage();
    const query = `goodsId=${goodsId}&beeperBtnId=${id}${open ? '&open=true' : ''}}`;
    wx.navigateTo({
      url: `/subcontract/home/pages/goodsDetail/index?${query}`
    });
  },
  // 选中日期
  selectDate(e) {
    const { item } = e.target.dataset;
    if (!item) return;
    this.setData({
      dateId: item.id,
      pageNo: 1
    });
    app.globalData['dock.date'] = item.date;
    this.getList();
  },
  back() {
    wx.navigateBack();
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    let { pageNo, totalPage } = this.data;
    if (totalPage <= pageNo) return;
    this.setData({
      pageNo: ++pageNo
    });
    this.getList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {}
});
